﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BugTracker.Core.Infrastructure.Exceptions;
using BugTacker.Common;
using BugTracker.WebUX.WebCode;
using BugTracker.BusinessEntities.Entities.BTUser;
using BugTracker.BusinessLogic.BTUser;
using Microsoft.Practices.Unity;
using System.Transactions;

namespace BugTracker.WebUX.Modules.Shared
{
    public partial class UCRegister : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region infobox

        private void SetInfoBoxError(string message)
        {
            //ActionInfoBox.Visible = true;
            //ActionInfoBox.Text = message;
            //ActionInfoBox.MessageType = InfoBox.InfoBox.MsgType.red;
        }

        #endregion

        protected void RegisterUserWizzard_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            try
            {
                WizardStep ws = this.RegisterUserWizzard.FindControl("UserInfoWizardStep") as WizardStep;
                if (ws != null)
                {
                    TextBox firstTB = ws.FindControl("FirstNameTextBox") as TextBox;
                    TextBox lastTB = ws.FindControl("LastNameTextBox") as TextBox;
                    TextBox workTB = ws.FindControl("WorkPhoneTextBox") as TextBox;
                    TextBox mobileTB = ws.FindControl("MobilePhoneTextBox") as TextBox;
                    TextBox emailTB = ws.FindControl("EmailTextBox") as TextBox;
                    TextBox detailTB = ws.FindControl("DetailTextBox") as TextBox;

                    var userId = BTSecurity.GetUserId();
                    if (!string.IsNullOrEmpty(userId))
                    {
                        var user = new BTUser();
                        user.BTUserID = 0;
                        user.UserId = Guid.Parse(userId);
                        user.FirstName = CommonRoutines.Encode(firstTB.Text);
                        user.LastName = CommonRoutines.Encode(lastTB.Text);
                        user.WorkPhone = CommonRoutines.Encode(workTB.Text);
                        user.MobilePhone = CommonRoutines.Encode(mobileTB.Text);
                        user.Email = CommonRoutines.Encode(emailTB.Text);
                        user.Detail = CommonRoutines.Encode(detailTB.Text);
                        user.IsActive = true;
                        user.IsDeleted = false;
                        user.RowGuid = Guid.NewGuid();
                        user.CreatedOn = DateTime.UtcNow;
                        user.ModifiedOn = DateTime.UtcNow;
                        Global.IoC.Container.Resolve<IBTUserService>().InsertObject(user);
                        System.Web.Security.Roles.AddUserToRole(BTSecurity.GetUserName(), "workspace");
                        Response.Redirect(BTHttpApplication.AppPaths.UserProfile);
                    }
                }
            }
            catch (BugTrackerExpection exc)
            {
                //TODO:Handler register exception
                SetInfoBoxError(exc.Message);
            }
        }

        protected void RegisterUserWizzard_CreatedUser(object sender, EventArgs e)
        {
            WizardStep ws = this.RegisterUserWizzard.FindControl("UserInfoWizardStep") as WizardStep;
            if (ws != null)
            {
                TextBox emailTB = ws.FindControl("EmailTextBox") as TextBox;
                if (emailTB != null)
                {
                    emailTB.Text = RegisterUserWizzard.Email;
                }
            }
        }
    }
}